package com.cennavi.mineplay.mineplay_data.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cennavi.mineplay.mineplay_data.model.transform.inter.Transform;

public class HttpTask extends Thread {

	private HttpConnectionInfo httpinfo;

	private List<Transform> trans;

	private String destDBJdbc;

	private String dbDriverClass;

	private String username;

	private String password;

	private String destTableNames;
	// public HttpTask(HttpConnectionInfo httpinfo,List<Transform> trans){
	// this.httpinfo = httpinfo;
	// this.trans = trans;
	// }

	private int company_id;

	private String url;

	private volatile boolean is_stop = false;

	public boolean isIs_stop() {
		return is_stop;
	}

	public void setIs_stop(boolean is_stop) {
		this.is_stop = is_stop;
	}

	public HttpTask(HttpConnectionInfo httpinfo, List<Transform> trans,
			String destDBJdbc, String dbDriverClass, String username,
			String password, String destTableNames) {
		super();
		this.httpinfo = httpinfo;
		this.trans = trans;
		this.destDBJdbc = destDBJdbc;
		this.dbDriverClass = dbDriverClass;
		this.username = username;
		this.password = password;
		this.destTableNames = destTableNames;
		this.company_id = this.httpinfo.getCompany_id();
		this.url = this.httpinfo.getUrl();
	}

	private List<TrackPoint> generateTps(String url) throws Exception {
		List<TrackPoint> tps = new ArrayList<TrackPoint>();

		CloseableHttpClient httpClient = HttpClients.createDefault();
		URIBuilder uriBuilder = new URIBuilder(url);
		HttpGet get = new HttpGet(uriBuilder.build());
		CloseableHttpResponse response = httpClient.execute(get);

		HttpEntity entity = response.getEntity();
		String string = EntityUtils.toString(entity, "utf-8");

		JSONObject json = JSONObject.parseObject(string);

		JSONArray data = json.getJSONArray("data");

		for (int i = 0; i < data.size(); i++) {
			JSONObject jo = data.getJSONObject(i);

			String car_number = jo.getString("car_number");
			double lng = jo.getDoubleValue("lng");
			double lat = jo.getDoubleValue("lat");
			double speed = jo.getDoubleValue("speed");
			double direct = jo.getDoubleValue("direct");
			long time = Long.parseLong(jo.getString("time"));
			
			
			String is_online_str = jo.getString("is_online");
			
			int is_online = 0;
			
			if ("1".equals(is_online_str)){
				is_online = 1;
			}else if ("ACC开".equals(is_online_str)){
				is_online = 1;
			}

			TrackPoint tp = new TrackPoint(car_number, company_id, lng, lat,
					speed, direct, time,is_online);
			
			for(Transform tran : trans){
				tran.trans(tp);
			}
			tps.add(tp);
		}

		return tps;
	}

	private Connection getDestConnection() throws Exception {
		Class.forName(this.dbDriverClass);
		Connection conn = DriverManager.getConnection(destDBJdbc, username,
				password);
		return conn;
	}

	public void run() {

		while (!is_stop) {
			try {
				
				Date date = new Date();
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
				
				String startTime = sdf.format(date);
				
				Date date2 = new Date(date.getTime() + (this.httpinfo.getFrequency() * 1000));
				
				String endTime = sdf.format(date2);
				
				
				
				Connection destConn = this.getDestConnection();

				List<TrackPoint> tps = this.generateTps(url+"?startTime="+startTime+"&endTime="+ endTime);

				ImportTrackPoint importTrackPoint = new ImportTrackPoint(
						destConn, tps, destTableNames);
				
				importTrackPoint.importData();
				
				destConn.close();
				
				Thread.sleep(this.httpinfo.getFrequency() * 1000);

			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}
	
	


}
